package com.study.web.jsoup;

import com.study.constant.CarConstant;
import com.study.utils.SysUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * jsoup 测试 登录 session /jsoup/pri
 */
@Log4j2
@RestController
@RequestMapping("/jsoup")
public class JsoupController {

    @GetMapping("sign")
    public String loginSing(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        System.out.println(SysUtils.getDateStrHms());
        session.setAttribute("JSOUP", "session测试s");
        Cookie cookie = new Cookie("sysName", "TestName");
        cookie.setMaxAge(CarConstant.TEMP_USER_COOKIE_TIMEOUT);
        response.addCookie(cookie);
        return "SUCCESS";
    }

    @GetMapping("logout")
    public String logout(HttpSession session) {
        System.out.println(SysUtils.getDateStrHms());
        session.removeAttribute("JSOUP");
        return "remove SUCCESS";
    }

    @GetMapping("/pri/date")
    public String getDate(HttpSession session) {
        System.out.println(SysUtils.getDateStrHms());
        System.out.println(SysUtils.getDateStrHms());
        System.out.println(SysUtils.getDateStrHms());
        return SysUtils.getDate4() + session.getAttribute("JSOUP");
    }

    /**
     * 测试
     *
     * @return
     */
    @GetMapping("/pri/pic")
    public String downPic() {
        try {
            FileUtils.copyURLToFile(new URL(""), new File(":"));
            log.info("消息");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return SysUtils.getDateSplit_();
    }
}
